import { Logger } from "../../../engine/Logger";
import ViewBase from "../../../engine/ViewBase";
import { ClubSocketClient } from "../../../net/ClubSocketClient";
import { ClubConfig } from "../../config/ClubConfig";
import { UserModel } from "../../model/UserModel";
import { UIHelper } from "../../util/UIHelper";


const { ccclass, property } = cc._decorator;

@ccclass
export default class ViewClubMemberQuanxian extends ViewBase {

    private quanxianList: Array<string>;
    private userid: number;
    private data: object

    public onInitChild(data: object) {
        this.data = data
        this.userid = data["userid"]
        let quanxianList: Array<string> = []
        let quanxian: string = data["quanxian"]
        if (quanxian && quanxian != "") {
            quanxianList = quanxian.split(",")
        }
        this.quanxianList = quanxianList;
        this.refresh()
    }

    private refresh() {
        Logger.log("quanxianList===", this.quanxianList)
        for (let i = 0; i < 8; i++) {
            let quanxianNode: cc.Node = this.findChild(this.node, "quanxianNode" + i)
            let selectNode: cc.Node = this.findChild(quanxianNode, "selectNode")
            selectNode.active = false
            UIHelper.onAddEvent(quanxianNode, () => {
                let index: number = this.quanxianList.indexOf((i + 1) + "")
                if (index == -1) {
                    this.quanxianList.push((i + 1) + "")
                } else {
                    this.quanxianList.splice(index, 1)
                }
                this.refresh()
            })
            if (this.quanxianList.includes((i + 1) + "")) {
                selectNode.active = true
            }
        }
        let btnsave: cc.Node = this.findChild(this.node, "btnsave")
        UIHelper.onAddEvent(btnsave, () => {
            ClubSocketClient.req_club_set_quanxian(UserModel.instance.nowClub.clubid, this.userid, this.quanxianList.toString(), () => {
                this.data["quanxian"] = this.quanxianList.toString()
                UIHelper.showTip("保存成功")
            })
        })
    }

}
